local canshi = fk.CreateSkill {
  name = "rmt__canshi"
}

Fk:loadTranslationTable{
  ["rmt__canshi"] = "蚕食",
  [":rmt__canshi"] = "①当一名角色使用基本牌或普通锦囊牌指定你为唯一目标时，若其有“傀”，你可取消之，然后其弃1枚“傀”。" ..
  "②你使用基本牌或普通锦囊牌仅选择一名角色为目标时，你可令任意名带有“傀”的角色也成为目标，然后这些角色弃1枚“傀”。",

  ["@@rmt__puppet"] = "傀",
  ["#rmt__canshi"] = "蚕食：你可取消【%arg】的目标，然后 %dest 弃“傀”",
  ["#rmt__canshi-targets"] = "蚕食：你可令任意名有“傀”的角色也成为目标，然后这些角色弃“傀”",

  ["$rmt__canshi1"] = "此患不足为惧，可蚕食而尽。",
  ["$rmt__canshi2"] = "小则蚕食，大则溃坝。",
}

canshi:addEffect(fk.TargetConfirming, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return
      target == player and
      player:hasSkill(canshi.name) and
      data:isOnlyTarget(player) and
      data.from:getMark("@@rmt__puppet") > 0 and
      (data.card.type == Card.TypeBasic or data.card:isCommonTrick())
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(
      player,
      {
        skill_name = canshi.name,
        prompt = "#rmt__canshi::" .. data.from.id .. ":" .. data.card.name
      }
    )
  end,
  on_use = function(self, event, target, player, data)
    ---@type string
    local skillName = canshi.name
    local room = player.room
    player:broadcastSkillInvoke(skillName, 1)
    room:notifySkillInvoked(player, skillName, "defensive")
    data:cancelTarget(data.to)
    room:removePlayerMark(data.from, "@@rmt__puppet")
  end,
})

canshi:addEffect(fk.AfterCardTargetDeclared, {
  mute = true,
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return
      target == player and
      player:hasSkill(canshi.name) and
      #(data.tos or {}) > 0 and
      data:isOnlyTarget(data.tos[1]) and
      (data.card.type == Card.TypeBasic or data.card:isCommonTrick())
  end,
  on_cost = function(self, event, target, player, data)
    local availableTargets = data:getExtraTargets({ bypass_times = true, bypass_distances = true })
    availableTargets = table.filter(availableTargets, function(p) return p:getMark("@@rmt__puppet") > 0 end)

    if #availableTargets == 0 then
      return false
    end

    local tos = player.room:askToChoosePlayers(
      player,
      {
        targets = availableTargets,
        min_num = 1,
        max_num = #availableTargets,
        prompt = "#rmt__canshi-targets",
        skill_name = canshi.name
      }
    )

    if #tos > 0 then
      event:setCostData(self, tos)
      return true
    end

    return false
  end,
  on_use = function(self, event, target, player, data)
    ---@type string
    local skillName = canshi.name
    local room = player.room
    player:broadcastSkillInvoke(skillName, 2)
    room:notifySkillInvoked(player, skillName, "special")
    local tos = event:getCostData(self)
    room:doIndicate(player, tos)
    for _, p in ipairs(tos) do
      data:addTarget(p)
      room:removePlayerMark(p, "@@rmt__puppet")
    end

    room:sendLog{
      type = "#AddTargetsBySkill",
      from = player.id,
      to = table.map(tos, Util.IdMapper),
      arg = skillName,
      arg2 = data.card:toLogString()
    }
  end
})

return canshi
